package cn.edu.hnu.homework.api.Controller;

import cn.edu.hnu.homework.api.Bean.Person;
import cn.edu.hnu.homework.api.Bean.User;
import cn.edu.hnu.homework.api.Mapper.PersonMapper;
import cn.edu.hnu.homework.api.Mapper.UserMapper;
import cn.edu.hnu.homework.api.Model.ResponseBean;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LogManager.getLogger(UserController.class);

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PersonMapper personMapper;
//    private Logger logger = LogManager.getLogger(this.getClass());

    @PostMapping("/login")
    public ResponseBean login(
            @RequestParam(value="username", required=false) String username,
            @RequestParam(value="password", required=false) String password) {
        logger.info("Enter UserController login");
        String msg = null;
        Boolean status= false;
        List<User> userList = userMapper.getByUsername(username);
        if(!userList.isEmpty()) {
            if(userList.get(0).getPassword().equals(password)) {
                status = true;
                msg = "登录成功";
            }
            else {
                status = false;
                // homework required
                msg = "登陆失败，密码错误";
            }
        }
        else {
            // homework required
            msg = "登陆失败,没有此用户";
        }
        ResponseBean response = new ResponseBean(status);
        response.setMsg(msg);
        return response;
    }

    @PostMapping("/register")
    public ResponseBean register(
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "username", required = true) String username,
            @RequestParam(value = "password", required = true) String password,
            @RequestParam(value = "age", required = true) int age,
            @RequestParam(value = "phone", required = true) String phone
            ) {
        logger.info("Enter RegisterController register");
        // 检查用户是否存在
        String msg = null;
        Boolean status= false;

        List<User> userList = userMapper.getByUsername(username);
        if (userList.isEmpty()) {
            //不存在就创建用户
            // TODO: 事务
            userMapper.insert(username, password);
            personMapper.insert(username, name, age, phone);
            // 不做判断直接返回
            status = true;
            msg = "创建用户成功";
        }
        else {
            status = false;
            msg = "用户已经存在";
        }
        ResponseBean responseBean= new ResponseBean(true);
        responseBean.setMsg(msg);
        return responseBean;
    }


    @PostMapping("/info")
    public ResponseBean getUserInfo(@RequestParam(value = "username", required = true) String username) {
        logger.info("Enter getUserInfo register" + username);
        String msg = null;
        Boolean status= false;
        ResponseBean responseBean = new ResponseBean(true);
        List<Person> peronList = personMapper.getByUsername(username);
//        List<String> datas = new ArrayList<String>();
        if(!peronList.isEmpty()) {
            String a = peronList.get(0).getName();
            logger.info(a);
            responseBean.setName(peronList.get(0).getName());
            responseBean.setPhone(peronList.get(0).getPhone());
            responseBean.setAge(peronList.get(0).getAge());
            responseBean.setUsername(peronList.get(0).getUsername());
            msg = "获取成功";
        }
        responseBean.setMsg(msg);
        return responseBean;
    }

    @PostMapping("/update")
    public ResponseBean updateUserInfo(
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "username", required = true) String username,
            @RequestParam(value = "age", required = true) int age,
            @RequestParam(value = "phone", required = true) String phone) {
        logger.info("Enter updateUserInfo update" + username);
        String msg = null;
        Boolean status= true;
        ResponseBean responseBean = new ResponseBean(status);
        personMapper.upateByUsername(username, name, age, phone);
        msg = "更新成功";
        responseBean.setMsg(msg);
        return responseBean;
    }
}
